Shader "Study/Chapter5/LightAtten"
{
    Properties
    {
		_diffuseIntensity("diffuseIntensity ",Range(0,20)) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

		Pass
        {
            Tags {"LightMode" = "ForwardBase"}

			  CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float3 worldNormal : TEXCOORD0;
                float3 worldPos : TEXCOORD1;
            };

            float _diffuseIntensity;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld,v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 ambient = unity_AmbientSky;  // 当前定义的环境光（天空盒的颜色看起来是）
                half Kd = _diffuseIntensity;
                fixed4 lightColor = _LightColor0;   // _LightColor0 默认定义的光照颜色
                float L =   _WorldSpaceLightPos0; // normalize(i.worldPos-_WorldSpaceLightPos0);
                float3 N = normalize(i.worldNormal) ;
                fixed4 diffuse = ambient + Kd * lightColor * max(0,dot(N,L));

                return diffuse;
            }
            ENDCG
		}

        Pass
        {
			Tags {"LightMode" = "ForwardAdd"}

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
			#include "AutoLight.cginc"
			#include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
				float3 normal : NORMAL;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
				float3 worldPos : TEXCOORD1;
				float3 worldNormal : TEXCOORD2;
            };

			sampler2D _LightTexture0;    // 光照衰减贴图
			float4x4 unity_WorldToLight;  // 顶点从何世界空间转到灯光下坐标  即当前点在光照下的位置
		

            v2f vert (appdata v)
            {
                v2f o;
				o.uv = v.uv;
                o.vertex = UnityObjectToClipPos(v.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld,v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
				// 将世界坐标从世界空间转到光源的坐标空间下
				float3 lightCoord = mul(unity_WorldToLight,float4(i.worldPos,1)).xyz;
				// atten 获取光照衰减 0~1 非线性
				fixed atten = tex2D(_LightTexture0,dot(lightCoord,lightCoord).rr);

				// AutoLight.cginc 内部获取光照衰减系数 
				//UNITY_LIGHT_ATTENUATION(atten,0,i.worldPos)

				fixed4 lightColor = _LightColor0 * atten;
				fixed3 N = normalize(i.worldNormal);
				fixed3 L = normalize(_WorldSpaceLightPos0 - i.worldPos);
				fixed4 diffuse = lightColor * max(0,dot(N,L));//lightColor * max(0,dot(N,L));

                return diffuse;
            }
            ENDCG
        }
    }
}
